package com.bbb.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bbb.common.Code;
import com.bbb.common.Result;
import com.bbb.pojo.User;
import com.bbb.pojo.UserLike;
import com.bbb.service.UserLikeService;
import com.bbb.service.UserService;
import com.bbb.common.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user/like")
public class UserLikeController {

    @Autowired
    private UserLikeService service;

    @Autowired
    private UserService userService;

    @RequestMapping("/unLike")
    @Transactional
    public synchronized Result unLike(@RequestParam("id") Integer id) {

        Integer userId = AuthUtil.getUserId();

        LambdaQueryWrapper<UserLike> userWorksWrapper = new LambdaQueryWrapper<>();
        userWorksWrapper.eq(UserLike::getUserId, userId);
        userWorksWrapper.eq(UserLike::getAuthorId, id);
        service.remove(userWorksWrapper);

        User user = userService.getById(id);
        try {
            user.setGz(new Integer(user.getGz()) - 1 + "");
        }catch (Exception e) {
            user.setGz(0 + "");
        }
        LambdaQueryWrapper<UserLike> userWorksWrapper1 = new LambdaQueryWrapper<>();
        userWorksWrapper1.eq(UserLike::getAuthorId, id);
        List<UserLike> list = service.list(userWorksWrapper1);
        List<Integer> collect = list.stream().map(UserLike::getUserId).distinct().collect(Collectors.toList());
        user.setFs(collect.size() + "");
        userService.updateById(user);

        return new Result(Code.GET_OK, "操作成功");
    }

    @RequestMapping("/like")
    @Transactional
    public synchronized Result like(@RequestParam("id") Integer id) {
        Integer userId = AuthUtil.getUserId();
        LambdaQueryWrapper<UserLike> userWorksWrapper = new LambdaQueryWrapper<>();
        userWorksWrapper.eq(UserLike::getUserId, userId);
        userWorksWrapper.eq(UserLike::getAuthorId, id);

        if (service.count(userWorksWrapper) > 0) {
            return new Result(Code.GET_OK, "操作成功");
        }
        UserLike like = new UserLike();
        like.setUserId(userId);
        like.setAuthorId(id);
        like.setCreateTime(new Date());
        service.save(like);
        User user = userService.getById(id);
        try {
            user.setGz(new Integer(user.getGz()) + 1 + "");
        }catch (Exception e) {
            user.setGz(1 + "");
        }
        LambdaQueryWrapper<UserLike> userWorksWrapper1 = new LambdaQueryWrapper<>();
        userWorksWrapper1.eq(UserLike::getAuthorId, id);
        List<UserLike> list = service.list(userWorksWrapper1);
        List<Integer> collect = list.stream().map(UserLike::getUserId).distinct().collect(Collectors.toList());
        user.setFs(collect.size() + "");
        userService.updateById(user);
        return new Result(Code.GET_OK, "操作成功");
    }

    @RequestMapping("/list")
    public Result list() {
        Integer userId = AuthUtil.getUserId();

        LambdaQueryWrapper<UserLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserLike::getUserId, userId);
        List<UserLike> list = service.list(wrapper);
        if (CollUtil.isEmpty(list)) {
            return new Result(Code.GET_OK, "查询成功", list);
        }

        List<User> users = userService.listByIds(list.stream().map(UserLike::getAuthorId).collect(Collectors.toList()));
        return new Result(Code.GET_OK, "查询成功", users);
    }
}
